package com.epam.web_project.bank.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.epam.web_project.bank.command.Command;
import com.epam.web_project.bank.command.CommandManager;
import com.epam.web_project.bank.exception.RegistrationException;
import com.epam.web_project.bank.util.AppPropertiesLoader;

public class Controller extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private CommandManager commandManager = CommandManager.getInstance();
	private static Logger logger = Logger.getLogger(Controller.class);
	private static AppPropertiesLoader props = AppPropertiesLoader
			.getInstance();

	public Controller() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Command command = commandManager.getCommand(request);
		String resPage = "";
		resPage = command.execute(request, response);
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(resPage);
		dispatcher.forward(request, response);

		// processRequest(request, response);

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Command command = commandManager.getCommand(request);
		String resPage = "";
		resPage = command.execute(request, response);
		response.sendRedirect(resPage);
		// processRequest(request, response);

	}

	/*
	 * private void processRequest(HttpServletRequest request,
	 * HttpServletResponse response) { try { //String resPage = null; Command
	 * command = commandManager.getCommand(request); String resPage =
	 * command.execute(request, response); Pattern
	 * pattern=Pattern.compile("/Bank/controller\\?command=show\\w+"); Matcher
	 * matcher=pattern.matcher(resPage); if(matcher.matches()) {
	 * response.sendRedirect(resPage); } else { RequestDispatcher dispatcher =
	 * getServletContext() .getRequestDispatcher(resPage);
	 * dispatcher.forward(request, response); } } catch (ServletException e) {
	 * logger.error(e); } catch (IOException e) { logger.error(e); } }
	 */

}
